#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :


import pymongo
import requests


class Aqiyi:
    def __init__(self):
        self.client = pymongo.MongoClient()
        self.collection = self.client['spiders10']['aqy']
        self.url = 'https://pcw-api.iqiyi.com/search/recommend/list?channel_id=2&data_type=1&mode=11&page_id={}&ret_num=48&session=45cd98db01208e99018b596ea82c3896&three_category_id=15;must'
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
        }

    def get_data(self, page):
        """
        这个是发送请求函的函数
        :param page:
        :return:
        """
        response = requests.get(url=self.url.format(page), headers=self.headers)
        return response.json()

    def parse_data(self, data):

        for node in data['data']['list']:
            item = {}
            item['title'] = node['title']
            item['playUrl'] = node['playUrl']
            item['description'] = node['description']
            print(item)
            self.save_data(item)

    def save_data(self, item):
        self.collection.insert_one(item)

    def run(self):
        for i in range(1, 2):
            res = self.get_data(i)
            self.parse_data(res)


if __name__ == '__main__':
    aqy = Aqiyi()
    aqy.run()


